package com.team.app.controller;


import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.team.app.model.Auth;
import com.team.app.model.Role;
import com.team.app.service.RoleService;
import com.team.app.util.ConvertUtil;


/**
 * Handles requests for the application home page.
 */
@Controller
public class RoleController extends AbstractController<Role>{

	private static final Logger logger = LoggerFactory.getLogger(RoleController.class);
	private final String PATH_BASE="/role";
	
	@Autowired	
	private RoleService roleService;
	
	private Long objId;
	
	
	//-----------------------------------------------------------------------------------
	@Override
	@RequestMapping(value = PATH_BASE, method = {RequestMethod.GET, RequestMethod.POST})
	public String indexAction(Model model,HttpSession s,Role obj) {	
		logger.info("RoleController index");
		if(!checkView(s, PATH_BASE)){
    		return accessDenied();
    	}  
		obj = obj != null ? obj : new Role();
		Auth loginAuth = (Auth)s.getAttribute("loginAuth");
		model.addAttribute("obj",obj);
		model.addAttribute("objs", roleService.findAll(loginAuth,obj));
		return PATH_BASE+"/index";
	}

	@Override
	@RequestMapping(value = PATH_BASE+"/add", method = {RequestMethod.GET, RequestMethod.POST})
	public String addAction(Model model,HttpSession s) {
		// TODO Auto-generated method stub
		if(!checkAdd(s, PATH_BASE)){
    		return accessDenied();
    	}  
		model.addAttribute("obj",new Role());
		return PATH_BASE+"/input";		
	}

	@Override
	@RequestMapping(value = PATH_BASE+"/edit/{id}", method = {RequestMethod.GET, RequestMethod.POST})
	public String editAction(Model model,HttpSession s, @PathVariable("id")Long id) {
		// TODO Auto-generated method stub
		if(!checkEdit(s, PATH_BASE)){
    		return accessDenied();
    	} 
		objId = id;
		model.addAttribute("obj",roleService.find(id));
		return PATH_BASE+"/input";
	}

	@Override
	@RequestMapping(value = PATH_BASE+"/confirm", method = {RequestMethod.GET, RequestMethod.POST})
	public String confirmAction(Model model,HttpSession s,@ModelAttribute("obj") Role obj, BindingResult result) {
		// TODO Auto-generated method stub
		model.addAttribute("obj", obj );
		boolean excute = true;
		if(ConvertUtil.isEmplty(obj.getRoleName())){
			setMessage(model,result,"roleName", "emp.email.blank");	
			excute = false;	
		}
		if(ConvertUtil.isEmplty(obj.getRoleDescription())){
			setMessage(model,result,"roleDescription", "role.dis.blank");	
			excute = false;	
		}
		if(!excute){
			return PATH_BASE+"/input";	
		}	
		
		return PATH_BASE+"/confirm";
	}

	@Override
	@RequestMapping(value = PATH_BASE+"/complete", method = {RequestMethod.GET, RequestMethod.POST})
	public String completeAction(Model model,HttpSession s, Role obj,BindingResult result) {
		// TODO Auto-generated method stub
		Auth loginAuth = (Auth)s.getAttribute("loginAuth");
		if(ConvertUtil.isEqual(obj.getRoleId(),objId) && !ConvertUtil.isNull(objId)){
			roleService.edit(obj);	
		}else{
			roleService.create(obj);
		}
		objId = null;
		model.addAttribute("objs", roleService.findAll(loginAuth,obj));
		return "redirect:"+PATH_BASE;
	}

	@Override
	@RequestMapping(value = PATH_BASE+"/delete/{id}" ,method = {RequestMethod.GET, RequestMethod.POST})
	public String deleteAction(Model model,HttpSession s, @PathVariable("id")Long id) {
		// TODO Auto-generated method stub
		if(!checkDelete(s, PATH_BASE)){
    		return accessDenied();
    	}  
		Auth loginAuth = (Auth)s.getAttribute("loginAuth");
		roleService.remove(roleService.find(id));		
		model.addAttribute("objs", roleService.findAll(loginAuth,null));
		return "redirect:"+PATH_BASE;
	}

	@Override
	@RequestMapping(value = PATH_BASE+"/back", method = {RequestMethod.GET, RequestMethod.POST})
	public String backAction(Model model, HttpSession s, Role obj) {
		// TODO Auto-generated method stub
		model.addAttribute("obj",obj);
		return PATH_BASE+"/input";
	}

	
}
